package com.wkhyapp.lm.view;

import android.graphics.Color;
import android.graphics.Rect;
import android.os.Build;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.loadmore.LoadMoreView;
import com.wkhyapp.lm.R;
import com.wkhyapp.lm.adapter.NoticeAdapter;
import com.wkhyapp.lm.adapter.UserCategoryAdapter;
import com.wkhyapp.lm.base.SuperActivity;
import com.wkhyapp.lm.contract.UserCategoryPresenter;
import com.wkhyapp.lm.contract.UserCategoryView;
import com.wkhyapp.lm.fragment.DTFragment;
import com.wkhyapp.lm.http.vo.Category;
import com.wkhyapp.lm.http.vo.Notice;
import com.wkhyapp.lm.utils.MemberUtils;
import com.wkhyapp.lm.utils.StatusBarFontUtil;
import com.wkhyapp.lm.utils.StringUtils;
import com.wkhyapp.lm.weigit.dialog.CategoryEditorDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class UserCategoryActivity extends SuperActivity<UserCategoryPresenter> implements UserCategoryView {


    @BindView(R.id.refresh_view)
    SwipeRefreshLayout refresh_view;
    @BindView(R.id.recycler_view)
    RecyclerView recycler_view;
    private UserCategoryAdapter categoryAdapter;
    private List<Category> categories;
    private int mCurrentPage = 1;
    @BindView(R.id.back_iv)
    ImageView back_iv;
    @BindView(R.id.add_rl)
    RelativeLayout add_rl;

    @Override
    public void initUI() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            translucentStatusBar();
        }
        StatusBarFontUtil.setLightStatusBarColor(this);

        categories = new ArrayList<>();
        categoryAdapter = new UserCategoryAdapter(R.layout.item_user_category,categories);
        refresh_view.setColorSchemeColors(Color.parseColor("#FFEC48"),Color.parseColor("#FFEC48"));
        GridLayoutManager manager = new GridLayoutManager(getContext(),2);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == recycler_view.getAdapter().getItemCount() - 1) {
                    return 2;
                }
                return 1;
            }
        });

        recycler_view.setLayoutManager(manager);
        int space = getResources().getDimensionPixelSize(R.dimen.item_space);
        recycler_view.addItemDecoration(new SpaceItemDecoration(space));

        recycler_view.setAdapter(categoryAdapter);
        refresh_view.setRefreshing(false);
        mPresenter.getCategory();
    }

    @Override
    protected void initListener() {
        categoryAdapter.setLoadMoreView(new LoadMoreView() {
            @Override
            public int getLayoutId() {
                return R.layout.view_index_load_more;
            }

            @Override
            protected int getLoadingViewId() {
                return com.chad.library.R.id.load_more_loading_view;
            }
            @Override
            protected int getLoadFailViewId() {
                return com.chad.library.R.id.load_more_load_fail_view;
            }
            @Override
            protected int getLoadEndViewId() {
                return com.chad.library.R.id.load_more_load_end_view;
            }
        });

        refresh_view.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mCurrentPage = 1;
                mPresenter.getCategory();
            }
        });

        back_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        add_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               final CategoryEditorDialog dialog = new CategoryEditorDialog(mContext,"添加分类",null);
                dialog.show();
                dialog.setCallBack(new CategoryEditorDialog.callBack() {
                    @Override
                    public void add(String name) {
                        if(StringUtils.isEmpty(name)){
                            TToast("请输入分类名称");
                            return;
                        }
                        dialog.close();
                         showLoad("");
                    mPresenter.addCategory(name);
                    }

                    @Override
                    public void update(String name, Category time) {

                    }
                });
            }
        });

        categoryAdapter.setCallBack(new UserCategoryAdapter.callBack() {
            @Override
            public void action(final Category item) {
                final CategoryEditorDialog dialog = new CategoryEditorDialog(mContext,"修改分类",item);
                dialog.show();
                dialog.setCallBack(new CategoryEditorDialog.callBack() {
                    @Override
                    public void add(String name) {
                    }

                    @Override
                    public void update(String name, Category time) {
                        if(StringUtils.isEmpty(name)){
                            TToast("请输入分类名称");
                            return;
                        }
                        dialog.close();
                        showLoad("");
                        mPresenter.updateCategory(name,item.getId());
                    }
                });
            }
        });
    }




    @Override
    public void setCategory(List<Category> list) {
        categories.clear();
        if(list!=null&&list.size()>0){
            categories.addAll(list);
        }
        categoryAdapter.notifyDataSetChanged();
        refresh_view.setRefreshing(false);
        categoryAdapter.loadMoreComplete();
        categoryAdapter.loadMoreEnd();
    }

    @Override
    public void setSucc() {
        dismissLoad();
        TToast("添加成功");
        mPresenter.getCategory();
    }


    @Override
    protected UserCategoryPresenter createPresenter() {
        return mPresenter = new UserCategoryPresenter(this);
    }

    @Override
    protected void afterCreate() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_user_category;
    }



    public class SpaceItemDecoration extends RecyclerView.ItemDecoration {
        private int space;
        public SpaceItemDecoration(int space) {
            this.space = space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            //不是第一个的格子都设一个左边和底部的间距
            outRect.left = space;
            outRect.bottom = space;
            //由于每行都只有3个，所以第一个都是3的倍数，把左边距设为0
            if (parent.getChildLayoutPosition(view) %3==0) {
                outRect.left = 0;
            }
        }

    }
}
